package com.yonyou.pmclouds.workquality.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.workquality.entity.WorkqualityCheckTypeEnum;
import com.yonyou.pmclouds.workquality.entity.WorkqualityClassifyVO;
import com.yonyou.pmclouds.workquality.entity.WorkqualityVO;
import com.yonyou.pmclouds.workquality.service.rmiitf.WorkqualityQueryService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/mobile/workquality")
@RestController
public class WorkqualityMobController {
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private WorkqualityQueryService queryService;

    //==================项目菜单的列表 企业在最上面的篮框 这个不知道用在哪里
    // 返回单据类型对应的所有检查类型的质量标准(检查类型是质量标准的一个属性)
    @GetMapping(value = "/search")
    public List<WorkqualityVO> search(@RequestParam(name = "tenantId") String tenantId, @RequestParam(name = "billType") String billType) throws BusinessException {
        // 单据类型的过滤
        int[] checkTypes = convertBillType2CheckType(billType);
        // 除了综合验收类型检查标准，其他都能查看到
        List<WorkqualityVO> workqualityVOs = queryService.queryByChecktypes(tenantId, checkTypes);

        //  重新调整单据类型的分类
        //检查标准分类
//        WorkqualityClassifyVO[] classifyVOS = classifyQuality(workqualityVOs);

        // 为了不修改前台，保留了原来的数据格式
//        Map<String, Object> map = new HashMap<>();
//        map.put("total", workqualityVOs.size());
//        map.put("result", workqualityVOs);
        return workqualityVOs;
    }

    private int[] convertBillType2CheckType(String billType) {
        int[] checkTypes = null;
        switch (billType) {
            case BillTypeConst.CONSTRUCTIONLOG_BILLTYPE:
                checkTypes = new int[]{WorkqualityCheckTypeEnum.GENERAL.getValue()};
                break;
            case BillTypeConst.SITEINSPECTION_BILLTYPE:
                checkTypes = new int[]{WorkqualityCheckTypeEnum.SITE_ACCEPTANCE.getValue(),
                        WorkqualityCheckTypeEnum.PARALLEL.getValue(), WorkqualityCheckTypeEnum.SITE.getValue(),
                        WorkqualityCheckTypeEnum.PATROL.getValue()};
                break;
            case BillTypeConst.PARALLEL_INSPECTION_BILLTYPE:
                checkTypes = new int[]{WorkqualityCheckTypeEnum.PARALLEL.getValue()};
                break;
            case BillTypeConst.SIDE_INSPECTION_BILLTYPE:
                checkTypes = new int[]{WorkqualityCheckTypeEnum.SITE.getValue()};
                break;
            case BillTypeConst.PATROL_BILLTYPE:
                checkTypes = new int[]{WorkqualityCheckTypeEnum.PATROL.getValue()};
                break;
            case BillTypeConst.STAGECHECK_BILLTYPE:
                checkTypes = new int[]{WorkqualityCheckTypeEnum.STAGE_CHECK.getValue()};
                break;
            default:
                checkTypes = new int[]{WorkqualityCheckTypeEnum.GENERAL.getValue(), WorkqualityCheckTypeEnum.SITE_ACCEPTANCE.getValue(),
                        WorkqualityCheckTypeEnum.PARALLEL.getValue(), WorkqualityCheckTypeEnum.SITE.getValue(),
                        WorkqualityCheckTypeEnum.PATROL.getValue()};
                break;
        }
        return checkTypes;
    }

    // 由于检查单据的拆分，不在需要对标准进行分组
    //建立检查类型与这一类检查类型对应的检查标准的关系
    private WorkqualityClassifyVO[] classifyQuality(List<WorkqualityVO> workqualityVOs) {
        WorkqualityClassifyVO[] classifyVOS = new WorkqualityClassifyVO[0];
        Map<String, List<WorkqualityVO>> checkTypeMap = new LinkedHashMap<>();
        for (WorkqualityVO vo : workqualityVOs) {
            List<WorkqualityVO> vos = checkTypeMap.get(vo.getChecktype());
            if (vos == null) {
                vos = new ArrayList<>();
                checkTypeMap.put(vo.getChecktype(), vos);
            }
            vos.add(vo);
        }
        if (!checkTypeMap.isEmpty()) {
            for (String key : checkTypeMap.keySet()) {
                WorkqualityClassifyVO classifyVO = new WorkqualityClassifyVO();
                classifyVO.setCheckType(key);
                classifyVO.setWorkqualityVOS(checkTypeMap.get(key));
                classifyVOS = ArrayUtils.add(classifyVOS, classifyVO);
            }
        }
        return classifyVOS;
    }


    @GetMapping(value = "/load")
    //填充检查标准javabean中工序的名字和检查项细节  应该是在现场模块用于展示用的 这个确定OK
    public WorkqualityVO load(@RequestParam(name = "pkWorkquality") String id) throws BusinessException {
        return queryService.queryByIDForMobile(id);
    }


    //这个是现场模块中供施工记录模块调用的质量标准 这个确定OK
    @GetMapping(value = "/prodDefault")
    public WorkqualityVO findByProcedure(@RequestParam(name = "procedure") String procedure,
                                         @RequestParam(name = "billType", defaultValue = BillTypeConst.CONSTRUCTIONLOG_BILLTYPE) String billType) {
        return queryService.queryByProcedureAndType(procedure, billType);
    }

    /**
     * 通过工序查询关联的检查标准
     *
     * @param procedure
     * @param billType
     * @param tenantId
     * @return
     */
    //================这个没看懂
    @GetMapping(value = "/getWorkQualityRange")
    public List<WorkqualityVO> findWorkQualityByProcedure(@RequestParam(name = "procedure") String procedure,
                                                          @RequestParam(name = "billType", defaultValue = BillTypeConst.CONSTRUCTIONLOG_BILLTYPE) String billType,
                                                          @RequestParam(name = "tenantId") String tenantId) throws BusinessException {
        if (StringUtils.isEmpty(procedure) || StringUtils.isEmpty(billType) || StringUtils.isEmpty(tenantId))
            return new ArrayList<WorkqualityVO>();

        return queryService.queryWorkQualityByProcedureAndType(procedure, billType, tenantId);
    }
}
